﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PTA.Image;

namespace PTA.Web
{
    /// <summary>
    /// 随机验证码生成控件
    /// </summary>
    [DefaultProperty("Text"), Description("绘制并生成随机验证码图片"), ToolboxData("<{0}:RandomCode runat=server></{0}:RandomCode>")]
    public class RandomCode : WebControl
    {
        private int textLength = 6;
        private int lineCount = 10;
        private int pointCount = 10;

        /// <summary>
        /// 验证码显示位数
        /// </summary>
        [DefaultValue("6"), Description("验证码显示位数"), Localizable(true), Category("Appearance"), Bindable(true)]
        public int TextLength
        {
            get
            {
                return this.textLength;
            }
            set
            {
                this.textLength = value;
            }
        }

        /// <summary>
        /// 验证码
        /// </summary>
        public static string Text
        {
            get
            {
                string text = (string)HttpContext.Current.Session["RandomCode"];
                return text ?? string.Empty;
            }
        }

        /// <summary>
        /// 画线数量
        /// </summary>
        [DefaultValue(10), Description("画线数量"), Localizable(true), Category("Appearance"), Bindable(true)]
        public int LineCount
        {
            get
            {
                return this.lineCount;
            }
            set
            {
                this.lineCount = value;
            }
        }

        /// <summary>
        /// 画点数量
        /// </summary>
        [DefaultValue(10), Description("画点数量"), Localizable(true), Category("Appearance"), Bindable(true)]
        public int PointCount
        {
            get
            {
                return this.pointCount;
            }
            set
            {
                this.pointCount = value;
            }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            if (this.Width.ToString() == "")
            {
                this.Width = new Unit(90);
            }
            if (this.Height.ToString() == "")
            {
                this.Height = new Unit(20);
            }
            if (HttpContext.Current.Request.QueryString["RandomCode"] != null)
            {
                HttpContext.Current.Session["RandomCode"] = PTA.Rand.NumberChar(TextLength);
                using (MemoryStream ms = ImageUtils.DrawTextImage((int)Width.Value, (int)Height.Value, Text, this.lineCount, this.pointCount, false))
                {
                    HttpContext.Current.Response.ClearContent();
                    HttpContext.Current.Response.ContentType = "image/jpeg";
                    HttpContext.Current.Response.BinaryWrite(ms.ToArray());
                    HttpContext.Current.Response.End();
                }
            }
        }

        /// <summary>
        /// 呈现控件
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            if (HttpContext.Current.Request.QueryString["RandomCode"] == null)
            {
                writer.Write(string.Concat(new object[] { "<span id='RandomCodeScope' style=\"border:1px solid #ccc;font-size:12px;padding:1px;\">生成中..</span><img  onload=\"document.getElementById('RandomCodeScope').style.display='none';\" src=\"?RandomCode=\" onclick=\"this.src='?RandomCode='+Math.random();\" style=\"cursor:hand;\" alt=\"点击刷新\" border=\"0\" align=\"absmiddle\" width=\"", this.Width, "\" height=\"", this.Height, "\"/>" }));
            }
        }
    }
}
